package cn.itcast.web.controller.cargo;

import cn.itcast.domain.cargo.ContractProductVo;
import cn.itcast.service.cargo.ContractService;
import cn.itcast.utils.DownloadUtil;
import cn.itcast.web.controller.BaseController;
import com.alibaba.dubbo.config.annotation.Reference;
import org.apache.commons.lang3.time.DateFormatUtils;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.ss.util.CellRangeAddress;
import org.apache.poi.xssf.streaming.SXSSFWorkbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletResponse;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.List;

@Controller
@RequestMapping("cargo/contract")
public class ContractPOIController extends BaseController {

    @Reference //dubbo注解
    ContractService contractService;

    @RequestMapping(value = "print",name = "跳转到出货表页面")
    public String print(){
        return "cargo/print/contract-print";
    }

    @RequestMapping(value = "printExcel2",name = "出货表模版打印")
    public void printExcel2(String inputDate, HttpServletResponse response) throws IOException {
        //1. 使用模版创建workbook
        Workbook wk = new XSSFWorkbook(session.getServletContext().getResourceAsStream("/make/xlsprint/tOUTPRODUCT.xlsx"));

        //2. 获取第一页
        Sheet sheet = wk.getSheetAt(0);

        //3. 获取第一行第一个单元格,设置内容
        Row row = sheet.getRow(0);
        Cell cell = row.getCell(1);
        cell.setCellValue(inputDate.replace("-","年")+"月份出货表");

        //4. 获取第三行中的每个单元格的样式,使用数组存储
        CellStyle[] cssArr = new CellStyle[9];
        row = sheet.getRow(2);
        for (int i = 1; i < row.getLastCellNum() ; i++) {
            cssArr[i] = row.getCell(i).getCellStyle();
        }

        //5. 遍历list集合,一个对象占一行,给每个单元格设置样式
        List<ContractProductVo> list = contractService.findByShipTime(inputDate, getCompanyId());
        if (list!=null && list.size()>0) {
            int index = 2;
            for (ContractProductVo vo : list) {
                row = sheet.createRow(index);

                //创建单元格,给每个单元格设置内容
                //客户
                cell = row.createCell(1);
                cell.setCellValue(vo.getCustomName());
                //设置样式
                cell.setCellStyle(cssArr[1]);

                //订单号
                cell = row.createCell(2);
                cell.setCellValue(vo.getContractNo());
                //设置样式
                cell.setCellStyle(cssArr[2]);

                //货号
                cell = row.createCell(3);
                cell.setCellValue(vo.getProductNo());
                //设置样式
                cell.setCellStyle(cssArr[3]);

                //数量
                cell = row.createCell(4);
                cell.setCellValue(vo.getCnumber());
                //设置样式
                cell.setCellStyle(cssArr[4]);

                //工厂
                cell = row.createCell(5);
                cell.setCellValue(vo.getFactoryName());
                //设置样式
                cell.setCellStyle(cssArr[5]);

                //工厂交期
                cell = row.createCell(6);
                cell.setCellValue(DateFormatUtils.format(vo.getDeliveryPeriod(),"yyyy-MM-dd"));
                //设置样式
                cell.setCellStyle(cssArr[6]);

                //船期
                cell = row.createCell(7);
                cell.setCellValue(DateFormatUtils.format(vo.getShipTime(),"yyyy-MM-dd"));
                //设置样式
                cell.setCellStyle(cssArr[7]);

                //贸易条款
                cell = row.createCell(8);
                cell.setCellValue(vo.getTradeTerms());
                //设置样式
                cell.setCellStyle(cssArr[8]);

                //索引+1
                index++;
            }
        }
        //6. 写回浏览器
        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        wk.write(bos);

        DownloadUtil.download(bos,response,inputDate.replace("-","年")+"月份出货表.xlsx");

    }


    @RequestMapping(value = "printExcel",name = "出货表打印")
    public void printExcel(String inputDate, HttpServletResponse response) throws IOException {
        //1 调用service查询出一个list集合
        List<ContractProductVo> list = contractService.findByShipTime(inputDate, getCompanyId());

        //2 创建一个工作薄
        //Workbook wk = new XSSFWorkbook();
        Workbook wk = new SXSSFWorkbook();//支持百万数据

        //3 创建表
        Sheet sheet = wk.createSheet();

        //3.1 合并标题行的单元格
        sheet.addMergedRegion(new CellRangeAddress(0,0,1,8));

        //3.2 设置lie列宽
        sheet.setColumnWidth(1,26*256);
        sheet.setColumnWidth(2,12*256);
        sheet.setColumnWidth(3,26*256);
        sheet.setColumnWidth(4,12*256);
        sheet.setColumnWidth(5,12*256);
        sheet.setColumnWidth(6,12*256);
        sheet.setColumnWidth(7,12*256);
        sheet.setColumnWidth(8,12*256);


        //4 创建标题行(行和列)
        Row row = sheet.createRow(0);
        //4.1 设置行高
        row.setHeightInPoints(36);

        Cell cell = row.createCell(1);
        //4.2 设置样式
        cell.setCellStyle(bigTitle(wk));

        // 2015-01
        cell.setCellValue(inputDate.replace("-","年")+"月份出货表");

        //5 创建列标题行(行和列)
        String[] titleArr = {"","客户","订单号","货号","数量","工厂","工厂交期","船期","贸易条款"};
        row = sheet.createRow(1);

        //5.1 设置行高
        row.setHeightInPoints(24);

        // 5.2 获取样式
        CellStyle titleCss =title(wk);

        for (int i = 1; i < titleArr.length; i++) {
            cell = row.createCell(i);
            cell.setCellValue(titleArr[i]);
            //设置样式
            cell.setCellStyle(titleCss);
        }

        //6 遍历list集合,每个vo对象占一行
        int index = 2;

        //6.1 获取样式
        CellStyle textCss = text(wk);

        if (list!=null && list.size()>0) {
            for (int i = 0; i < 6000; i++) {
                for (ContractProductVo vo : list) {
                    row = sheet.createRow(index);
                    //设置行高
                    row.setHeightInPoints(24);

                    //创建单元格,给每个单元格设置内容
                    //客户
                    cell = row.createCell(1);
                    cell.setCellValue(vo.getCustomName());
                    //设置样式
                    cell.setCellStyle(textCss);

                    //订单号
                    cell = row.createCell(2);
                    cell.setCellValue(vo.getContractNo());
                    //设置样式
                    cell.setCellStyle(textCss);

                    //货号
                    cell = row.createCell(3);
                    cell.setCellValue(vo.getProductNo());
                    //设置样式
                    cell.setCellStyle(textCss);

                    //数量
                    cell = row.createCell(4);
                    cell.setCellValue(vo.getCnumber());
                    //设置样式
                    cell.setCellStyle(textCss);

                    //工厂
                    cell = row.createCell(5);
                    cell.setCellValue(vo.getFactoryName());
                    //设置样式
                    cell.setCellStyle(textCss);

                    //工厂交期
                    cell = row.createCell(6);
                    cell.setCellValue(DateFormatUtils.format(vo.getDeliveryPeriod(),"yyyy-MM-dd"));
                    //设置样式
                    cell.setCellStyle(textCss);

                    //船期
                    cell = row.createCell(7);
                    cell.setCellValue(DateFormatUtils.format(vo.getShipTime(),"yyyy-MM-dd"));
                    //设置样式
                    cell.setCellStyle(textCss);

                    //贸易条款
                    cell = row.createCell(8);
                    cell.setCellValue(vo.getTradeTerms());
                    //设置样式
                    cell.setCellStyle(textCss);

                    //索引+1
                    index++;
                }
            }
        }

        //7 将wk通过response写回浏览器
        //download(ByteArrayOutputStream byteArrayOutputStream, HttpServletResponse response, String returnName)
        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        wk.write(bos);

        DownloadUtil.download(bos,response,inputDate.replace("-","年")+"月份出货表.xlsx");
    }


    //大标题的样式
    public CellStyle bigTitle(Workbook wb){
        CellStyle style = wb.createCellStyle();
        Font font = wb.createFont();
        font.setFontName("宋体");
        font.setFontHeightInPoints((short)16);
        font.setBold(true);//字体加粗
        style.setFont(font);
        style.setAlignment(HorizontalAlignment.CENTER);				//横向居中
        style.setVerticalAlignment(VerticalAlignment.CENTER);		//纵向居中
        return style;
    }

    //小标题的样式
    public CellStyle title(Workbook wb){
        CellStyle style = wb.createCellStyle();
        Font font = wb.createFont();
        font.setFontName("黑体");
        font.setFontHeightInPoints((short)12);
        style.setFont(font);
        style.setAlignment(HorizontalAlignment.CENTER);				//横向居中
        style.setVerticalAlignment(VerticalAlignment.CENTER);		//纵向居中
        style.setBorderTop(BorderStyle.THIN);						//上细线
        style.setBorderBottom(BorderStyle.THIN);					//下细线
        style.setBorderLeft(BorderStyle.THIN);						//左细线
        style.setBorderRight(BorderStyle.THIN);						//右细线
        return style;
    }

    //文字样式
    public CellStyle text(Workbook wb){
        CellStyle style = wb.createCellStyle();
        Font font = wb.createFont();
        font.setFontName("Times New Roman");
        font.setFontHeightInPoints((short)10);

        style.setFont(font);

        style.setAlignment(HorizontalAlignment.LEFT);				//横向居左
        style.setVerticalAlignment(VerticalAlignment.CENTER);		//纵向居中
        style.setBorderTop(BorderStyle.THIN);						//上细线
        style.setBorderBottom(BorderStyle.THIN);					//下细线
        style.setBorderLeft(BorderStyle.THIN);						//左细线
        style.setBorderRight(BorderStyle.THIN);						//右细线

        return style;
    }
}
